//工作台的接口数据
import Mock from 'mockjs'
import { param2Obj } from '../utils/urlutils.js'

//生成随机号码
const { Random } = require('mockjs');
Random.extend({
    phonenumber: function(string) {
        var prefixs = new Array("139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152", "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185", "176", "133", "153", "189", "180", "181", "177")
        var phone = prefixs[parseInt(Math.random() * prefixs.length)]
        for (var i = 0; i < 8; i++) {
            phone = phone + Math.floor(Math.random() * 10)
        }
        return phone
    }
});

var list = [];
for (var i = 0; i < 100; i++) {
    list.push({
        id: Mock.mock('@guid()'),
        username: Mock.mock('@cname()'),
        doctorname: Mock.mock('@cname()'),
        status: Mock.mock('@natural(0, 2)'),
        sextype: Mock.mock('@natural(0, 1)'),
        age: Mock.mock('@natural(18, 80)'),
        vip: Mock.mock('@natural(1, 10)'),
        date: Mock.mock('@datetime()'),
        tel: Random.phonenumber(),
    })
}
export default {
    getWorkData: config => {
        var { keyword, page, pageSize, statusnum, stime, etime } = param2Obj(config.url);
        var res1;
        if (stime && etime) {
            res1 = list.filter(item => +new Date(item.date) > stime && +new Date(item.date) < etime);

        } else {
            res1 = list;
        }
        var res2
        if (statusnum) {
            res2 = res1.filter(item => item.status == statusnum);
        } else {
            res2 = res1
        }
        var res = res2.filter(item => item.username.indexOf(keyword) != -1);
        var resLimit = res.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize;
        })
        return {
            code: 200,
            msg: '获取工作台数据成功',
            data: resLimit,
            total: res.length
        }
    }
}